<?php
class H_Base extends H_B{
    protected $_noneDelId = array();
    protected $_catentity; // Is CatEntity Object for all Class use interface to set it
    //------------------------------------------------
    public function updateCat($entity_id,$catArray){
        $this->_catentity->delbyentity($entity_id);        
        foreach ($catArray as $cat){
            $data = array('entity_id' => $entity_id,'cate_id' => $cat);
            $this->_catentity->add($data);
        }
    }

    public function filterData(&$data,&$cat = null){        
        if (isset($data['keyword'])){
            $data['keyword'] = implode(":", $data['keyword']);
        }
        if (isset($data['cat'])) $cat = $data['cat'];
        foreach ($data as $key => $item){
            if (!in_array($key, $this->_getCols())){
                unset($data[$key]);
            }else{
                if ( get_magic_quotes_gpc())
                    $data[$key] = htmlspecialchars( stripslashes($data[$key]),ENT_QUOTES, 'UTf-8');
                else
                    $data[$key] = htmlspecialchars( $data[$key],ENT_QUOTES, 'UTf-8');
            }
        }        
    }

    public function add($data){
        $this->urlProcess($data);
        $this->filterData($data,$cat);
        $this->insert($data);
        $lastId = $this->_db->lastInsertId();      
        $this->updateCat($lastId, $cat);
        return $lastId;
    }
    public function getAllUrl($url){
        $select = $this->_db->select();
        $select->from($this->_name,array('url'))
                ->where("url like ?","%".$url);
        return $this->_db->fetchAll($select,Zend_Db::FETCH_ASSOC);
    }
    public function edit($id,$data){
        $this->urlProcess($data);
        $this->filterData($data,$cat);
        $this->updateCat($id, $cat);
        $where = $this->_db->quoteInto("id = ?", $id);
        return $this->update($data, $where);
    }

    public function del($id){
            $where = $this->_db->quoteInto("id = ?", $id);
            return $this->delete($where);
    }

    public function isCanNotDel($id){
            return in_array($id , $this->_noneDelId);
    }
    public function existsUrl($url){
        $select = $this->_db->select();
        $select->from($this->_name,array('url'))
                ->where("url = ?",$url);
        $data = $this->_db->fetchAll($select,Zend_Db::FETCH_ASSOC);
        return (count($data)>0);
    }
    protected function urlProcess(&$data){
        $url = $this->change($data['title']);
    	$allUrl = $this->getAllUrl($url);    	 	
        $count = count($allUrl);
        if ($count>0){
            if ($this->existsUrl($url))
            $data['url'] = $count."-".$url;
        }else{       
            $data['url'] = $url;
        }
    }

    private function change($string){
	$string = preg_replace("/á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ/","a", $string);
    	$string = preg_replace("/Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ/","A", $string);
    	$string = preg_replace("/é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ/","e", $string);
    	$string = preg_replace("/É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ/","E", $string);
    	$string = preg_replace("/í|ì|ỉ|ĩ|ị/","i", $string);
    	$string = preg_replace("/Í|Ì|Ỉ|Ĩ|Ị/","I", $string);
        $string = preg_replace("/ý|ỳ|ỷ|ỹ|ỵ/","y", $string);
        $string = preg_replace("/Ý|Ỳ|Ỷ|Ỹ|Ỵ/","Y", $string);
    	$string = preg_replace("/o|ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ/","o", $string);
    	$string = preg_replace("/O|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ/","O", $string);
    	$string = preg_replace("/u|ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự/","u", $string);
    	$string = preg_replace("/U|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự/","U", $string);
    	$string = preg_replace("/đ/","d", $string);
    	$string = preg_replace("/Đ/","D", $string);
    	$string = preg_replace("/[^a-zA-Z0-9\s-]+/","", $string);
    	$string = preg_replace("/\s+/","-", $string);
    	$string = preg_replace("/-+/","-", $string);
    	$string = strtolower($string);
    	return $string;
    }
}